using UnityEngine;

public class ladder : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            collision.gameObject.GetComponent<state>().isOnLadder = true;
        }
    }
    private void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            playerControl player = collision.gameObject.GetComponent<playerControl>();
            state state = collision.gameObject.GetComponent<state>();
            collision.gameObject.GetComponent<state>().isOnLadder = false;

            if (!player._controller.isGrounded)
            {
                state.movementState = state.MovementState.FLOATING;
                state.canClimb = false;
            }
            else
            {
                state.movementState = state.MovementState.IDLE;

            }
        }

    }
    private void OnTriggerStay2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Player")
        {

            JumpWhileClimb(collision);
            climb(collision);
        }

    }
    /// <summary>
    /// 当角色在梯子上按下跳跃键及其他方向键时，处理其跳跃逻辑
    /// </summary>
    /// <param name="collision"></param>
    private void JumpWhileClimb(Collider2D collision)
    {
        playerControl player = collision.gameObject.GetComponent<playerControl>();
        KeyCode jump = player.jump;
        KeyCode up = player.up;
        KeyCode down = player.down;
        KeyCode left = player.left;
        KeyCode right = player.right;
        state state = collision.gameObject.GetComponent<state>();
        
        if (Input.GetKey(jump) && (Input.GetKey(left) && (!Input.GetKey(up)) && state.movementState == state.MovementState.CLIMBING))
        {
            player.Jump(player.jumpHeight / 3);
            state.canClimb = false;
            Debug.Log("jump from ladder");

        }
        else if (Input.GetKey(jump) && (Input.GetKey(right) && (!Input.GetKey(up)) && state.movementState == state.MovementState.CLIMBING))
        {
            player.Jump(player.jumpHeight / 3);
            state.canClimb = false;
            Debug.Log("jump from ladder");

        }
        else if (Input.GetKey(jump) && (Input.GetKey(down) && (!Input.GetKey(up)) && state.movementState == state.MovementState.CLIMBING))
        {
            player.Jump(player.jumpHeight / 5);
            state.canClimb = false;
            Debug.Log("jump from ladder");

        }
        else if (Input.GetKey(jump) && (Input.GetKey(left) && (Input.GetKey(up)) && state.movementState == state.MovementState.CLIMBING))
        {
            player.Jump(player.jumpHeight);
            state.canClimb = false;
            Debug.Log("jump from ladder");

        }
        else if (Input.GetKey(jump) && (Input.GetKey(right) && (Input.GetKey(up)) && state.movementState == state.MovementState.CLIMBING))
        {
            player.Jump(player.jumpHeight);
            state.canClimb = false;
            Debug.Log("jump from ladder");

        }


    }
    /// <summary>
    /// 处理攀爬楼梯的移动
    /// </summary>
    /// <param name="collision"></param>
    private void climb(Collider2D collision)
    {
        playerControl player = collision.gameObject.GetComponent<playerControl>();
        KeyCode jump = player.jump;
        KeyCode up = player.up;
        KeyCode down = player.down;
        KeyCode left = player.left;
        KeyCode right = player.right;
        state state = collision.gameObject.GetComponent<state>();


        if (Input.GetKey(down) && !Input.GetKey(jump))
        {
            // 如果当前在梯子旁边,但是还没有攀爬的状态下按下键
            if (
                    (
                    state.movementState == state.MovementState.IDLE
                    || state.movementState == state.MovementState.RUNNING
                    || state.movementState == state.MovementState.WALKING
                    || state.movementState == state.MovementState.FLOATING
                    )
                && state.canClimb
                )
            {
                state.movementState = state.MovementState.CLIMBING;
                state.resetMultiJumpCnt();

            }
            if (state.movementState == state.MovementState.CLIMBING)
            {
                player.normalizedVerticalSpeed = -1;

            }
        }
        else if (Input.GetKey(up) && !Input.GetKey(jump))
        {
            // 如果当前在梯子旁边,但是还没有攀爬的状态下按下键
            if (
                    (
                    state.movementState == state.MovementState.IDLE
                    || state.movementState == state.MovementState.RUNNING
                    || state.movementState == state.MovementState.WALKING
                    || state.movementState == state.MovementState.FLOATING
                    )
                && state.canClimb
                )
            {
                state.movementState = state.MovementState.CLIMBING;
                state.resetMultiJumpCnt();

            }
            if (state.movementState == state.MovementState.CLIMBING)
            {
                player.normalizedVerticalSpeed = 1;

            }

        }
        else
        {
            if (state.movementState == state.MovementState.CLIMBING)
            {
                player.normalizedVerticalSpeed = 0;

            }

        }
    }
}
